// define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
//
//     var Controller = {
//         index: function () {
//             // 初始化表格参数配置
//             Table.api.init({
//                 extend: {
//                     index_url: 'purchase/requisition/index' + location.search,
//                     add_url: 'purchase/requisition/add',
//                     edit_url: 'purchase/requisition/edit',
//                     del_url: 'purchase/requisition/del',
//                     multi_url: 'purchase/requisition/multi',
//                     table: 'purchase_requisition',
//                 }
//             });
//
//             var table = $("#table");
//
//             // 初始化表格
//             table.bootstrapTable({
//                 url: $.fn.bootstrapTable.defaults.extend.index_url,
//                 pk: 'id',
//                 sortName: 'id',
//                 columns: [
//                     [
//                         {checkbox: true},
//                         {field: 'id', title: __('Id')},
//                         {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
//                         {field: 'number', title: __('Number')},
//                         {field: 'admin_id', title: __('Admin_id')},
//                         {field: 'total_num', title: __('Total_num')},
//                         {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
//                         {field: 'is_execute', title: __('Is_execute')},
//                         {field: 'is_finish', title: __('Is_finish')},
//                         {field: 'is_end', title: __('Is_end')},
//                         {field: 'explain', title: __('Explain')},
//                         {field: 'abstract', title: __('Abstract')},
//                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
//                     ]
//                 ]
//             });
//
//             // 为表格绑定事件
//             Table.api.bindevent(table);
//         },
//         add: function () {
//             Controller.api.bindevent();
//         },
//         edit: function () {
//             Controller.api.bindevent();
//         },
//         api: {
//             bindevent: function () {
//                 Form.api.bindevent($("form[role=form]"));
//             }
//         }
//     };
//     return Controller;
// });
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            this.table.first();
            this.table.second();

        },

        edit: function () {
            Controller.api.bindevent();
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                Table.api.init({
                extend: {
                    // index_url: 'purchase/requisition/index' + location.search,
                    add_url: 'purchase/requisition/adds',
                    // edit_url: 'purchase/requisition/edit',
                    // del_url: 'purchase/requisition/del',
                    // multi_url: 'purchase/requisition/multi',
                    // table: 'purchase_requisition',
                }
            });
                $(".btn-add").data('area',["80%",'80%']);

                table1.bootstrapTable({
                    url: 'purchase/requisition/table1',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'id', title: 'ID'},
                            {field: 'createtime', title: __('Createtime')},
                            {field: 'number', title: __('Number')},
                            {field: 'nickname', title: __('Nickname')},
                            {field: 'name', title: __('Name')},

                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, buttons: [
                                    {
                                        name: 'log',
                                        title: '商品列表',
                                        text: '商品列表',
                                        icon: 'fa fa-list',
                                        classname: 'btn btn-primary btn-xs btn-click',
                                        click: function (e, data) {
                                            $("#myTabContent2 .form-commonsearch input[name='number']").val(data.id);
                                            $("#myTabContent2 .btn-refresh").trigger("click");
                                        }
                                    }
                                ], formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'purchase/requisition/table2',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'id', title: 'ID', operate: false},
                            // {field: 'username', title: __('Nickname')},
                            {field: 'title', title: __('Title'), operate: false},
                            // {field: 'url', title: __('Url'), align: 'left', formatter: Table.api.formatter.url},
                            //自定义栏位,custom是不存在的字段
                            {field: 'number', title: __('请购单ID'),  formatter: Controller.api.formatter.custom,visible: false},
                            {field: 'specs', title: __('Specs'), operate: false},
                            {field: 'requisition_num', title: __('RequisitionNum'),  operate: 'RANGE', sortable: true, operate: false},
                            {field: 'price', title: __('Price'),  operate: 'RANGE', sortable: true, operate: false},
                            {field: 'remark', title: __('Remark'), operate: false},

                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                browser: function (value, row, index) {
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser">' + row.useragent.split(" ")[0] + '</a>';
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success"  data-id="' + row.id + '"><i class="fa ' + (row.title == '' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
                browser: {
                    'click .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
                    }
                },
            }
        }

    };
    return Controller;
});